package com.woniu.service.impl;

import com.woniu.entity.AllMenu;
import com.woniu.mapper.AllMenuMapper;
import com.woniu.service.AllMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhaoj
 * @Date 2025/6/16 03:40
 * @Description TODO
 **/
@Service
public class AllMenuServiceImpl implements AllMenuService {
    @Autowired
    private AllMenuMapper allMenuMapper;
    @Override
    public List<AllMenu> getById(Integer userId) {
        List<AllMenu> rawList = allMenuMapper.selectMenuByUid(userId);
        List<AllMenu> resultList = new ArrayList<>();
        for (AllMenu menu : rawList) {
            if (menu.getParentId() == 0) {
                resultList.add(menu);
            }
        }
        for (AllMenu menu1 : resultList) {
            List<AllMenu> menuLayer2 = new ArrayList<>();
            for (AllMenu menu : rawList) {
                if (menu.getParentId().equals(menu1.getMenuId())) {
                    menuLayer2.add(menu);
                }
            }
            menu1.setSubMenu(menuLayer2);
        }
        return resultList;
    }

    @Override
    public List<AllMenu> getRouteById(Integer userId) {
        List<AllMenu> rawList = allMenuMapper.selectMenuByUid(userId);
        List<AllMenu> resultList = new ArrayList<>();
        for (AllMenu menu : rawList) {
            if (menu.getParentId() != 0) {
                resultList.add(menu);
            }
        }
        return resultList;
    }

//    public List<AllMenu> getSubMenuByLayer(List<AllMenu> allMenuList,Integer layer){
//        List<AllMenu> pList = new ArrayList<>();
//        for (AllMenu menu : allMenuList) {
//            if (menu.getParentId() == layer) {
//                pList.add(menu);
//            }
//        }
//        if(pList.isEmpty()){
//            return null;
//        }
//        for (AllMenu menu : pList) {
//            menu.setSubMenu(getSubMenuByLayer(allMenuList,layer+1));
//        }
//
//    }
}
